/**
* @author:kellen
* @contact:xiekellen@hotmail.com
* @company:achivo-gz
* @created on:
* @modified on:
*/

package com.sasis.dao.ibatis;

import java.util.List;

import com.sasis.dao.ibatis.BaseDaoiBATIS;
import com.sasis.model.FoodType;
import com.sasis.dao.FoodTypeDao;

import org.springframework.orm.ObjectRetrievalFailureException;

public class FoodTypeDaoiBatis extends BaseDaoiBATIS implements FoodTypeDao {

    /**
     * @see com.sasis.dao.FoodTypeDao#getFoodTypes(com.sasis.model.FoodType)
     */
    public List getFoodTypes(final FoodType foodType) {
          return getSqlMapClientTemplate().queryForList("getFoodTypes", foodType);
    }

    /**
     * @see com.sasis.dao.FoodTypeDao#getFoodType(Integer id)
     */
    public FoodType getFoodType(Integer id) {
        FoodType foodType = (FoodType) getSqlMapClientTemplate().queryForObject("getFoodType", id);

        if (foodType == null) {
            throw new ObjectRetrievalFailureException(FoodType.class, id);
        }

        return foodType;
    }

    /**
     * @see com.sasis.dao.FoodTypeDao#saveFoodType(FoodType foodType)
     */    
    public void saveFoodType(final FoodType foodType) {
        Integer id = foodType.getId();
        // check for new record
        if (id == null) {
            id = (Integer) getSqlMapClientTemplate().insert("addFoodType", foodType);
        } else {
            getSqlMapClientTemplate().update("updateFoodType", foodType);
        }
        if( id == null ) {
            throw new ObjectRetrievalFailureException(FoodType.class, id);
        }
    }

    /**
     * @see com.sasis.dao.FoodTypeDao#removeFoodType(Integer id)
     */
    public void removeFoodType(FoodType foodType) {
        getSqlMapClientTemplate().update("deleteFoodType",  foodType);
    }
}
